<template>
	<view>
		<view class="goods-list">
			<view v-for="(item,index) in goodsList" :key="index"  @click="gotoDetail(item)">
				<my-goods :goods="item" ></my-goods>
			</view>
		</view>
	</view>
</template>

<script setup>
import {onLoad,onReachBottom,onPullDownRefresh} from '@dcloudio/uni-app'
import { ref } from 'vue';
const queryObj = ref({
	query:'', //查询关键字
	cid:'', //商品分类Id
	pagenum:1, //页码值
	pagesize:10 //每页显示多少条数据
})
const goodsList = ref([])
const total = ref(0)
const isLoading = ref(false)
onLoad((options)=>{
	console.log(queryObj.value)
	queryObj.value.query = options.query || ''
	queryObj.value.cid = options.cid || ''
	getGoodsList()
})
onReachBottom(()=>{
	if(queryObj.pagenum * queryObj.pagesize >= total.value) return uni.$showMsg('数据加载完毕！')
	if(isLoading.value) return
	queryObj.value.pagenum+=1
	getGoodsList()
})
onPullDownRefresh(()=>{
	queryObj.value.pagenum = 1
	total.value = 0
	isLoading.value =false
	goodsList.value = []
	getGoodsList(()=>uni.stopPullDownRefresh())
})
const getGoodsList = async(cb) => {
	isLoading.value = true
	const { data: res } = await uni.$http.get('/api/public/v1/goods/search', queryObj.value)
	isLoading.value = false
	cb && cb()
	if (res.meta.status !== 200) return uni.$showMsg()	goodsList.value = [...goodsList.value,...res.message.goods]
	total.value = res.message.total
}
const gotoDetail = (item) => {
		uni.navigateTo({
			url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
		})
}

</script>

<style lang="scss" scoped>

</style>
